using System.IO; 
using System.Text; 
using UnityEngine;

namespace XFGameFramework
{
    /// <summary>
    /// Excel转换Json的工具类
    /// </summary>
    public class Excel2JsonTool
    {
        /// <summary>
        /// 把excel转成json
        /// </summary>
        /// <param name="excelPath">excel文件路径</param>
        /// <param name="jsonPath">转换后json文件的路径</param>
        /// <param name="header">表中有几行是表头</param>
        /// <param name="exclude_prefix">导出时，排除掉包含指定前缀的列</param>
        public static void ExcelToJson(string excelPath, string jsonPath, int header, string exclude_prefix)
        {

            if (!File.Exists(excelPath))
                throw new System.Exception(string.Format("文件不存在:{0}", excelPath));

            if (exclude_prefix == null) exclude_prefix = string.Empty;

            ExcelLoader excel = new ExcelLoader(excelPath, header);
            JsonExporter json = new JsonExporter(excel, false, true, string.Empty, false, header, exclude_prefix, false, false);

            json.SaveToFile(jsonPath, Encoding.UTF8);
        }


        /// <summary>
        /// 把某个目录下所有的 excel 文件转成 json
        /// </summary>
        /// <param name="excelDir">excel所在的目录</param>
        /// <param name="pattern">匹配excel的模式,例如:  "*.xls" 或  "*.xlsx"</param>
        /// <param name="jsonDir">转换结束存放json的目录</param>
        /// <param name="header">表中有几行是表头</param>
        /// <param name="exclude_prefix">导出时，排除掉包含指定前缀的列</param>
        /// <exception cref="System.Exception"></exception>
        public static void ExcelToJson(string excelDir, string pattern ,string jsonDir ,int header, string exclude_prefix) {

            if (!Directory.Exists(excelDir))
                throw new System.Exception(string.Format("文件夹不存在:{0}", excelDir));

            if (!Directory.Exists(jsonDir))
                Directory.CreateDirectory(jsonDir);

            string[] files = Directory.GetFiles(excelDir, pattern, SearchOption.AllDirectories);

            for (int i = 0; i < files.Length; i++)
            {
                UnityEditor.EditorUtility.DisplayProgressBar("导出json", string.Format("Excel:{0}", files[i]), (float)i / files.Length);

                try
                {
                    if (Path.GetFileName(files[i]).StartsWith("~$")) continue; // 以~$ 开头的是临时文件
                    ExcelToJson(files[i], GetPath(jsonDir, Path.GetFileNameWithoutExtension(files[i])), header, exclude_prefix);
                    Debug.LogFormat("导出Excel:{0}", files[i]);
                }
                catch (System.Exception e)
                {
                    Debug.LogErrorFormat("excel:{0} export error:{1}",files[i],e.ToString());
                }
            
            }
            UnityEditor.EditorUtility.ClearProgressBar();
            Debug.Log("导出完成!");
        }

        private static string GetPath(string dir, string fileName) {
            if (dir.EndsWith("/"))
                return string.Format("{0}{1}.json", dir, fileName);
            else
                return string.Format("{0}/{1}.json", dir, fileName);
        }


    }
}

